package com.mvp.lt.airlineview.TaskTest;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mvp.lt.airlineview.R;
import com.amap.api.maps.model.LatLng;
import com.mvp.lt.airlineview.TimerTask.base.TaskPriority;
import com.mvp.lt.airlineview.utils.CRC16;
import com.mvp.lt.airlineview.utils.LatLngUtils;
import com.mvp.lt.airlineview.utils.RouteUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * $activityName
 *
 * @author LiuTao
 * @date 2019/1/5/005
 */


public class TaskTestActivity extends AppCompatActivity {

    private TextView mShowTask;
    private StringBuffer mStringBuffer;
    public List<LatLng> mLatLngLists = new ArrayList<>();// 航点

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_test);
        mShowTask = findViewById(R.id.show_task);
        mStringBuffer = new StringBuffer();
    }

    public void low(View view) {
        new LogTask(this, "123")
                .setPriority(TaskPriority.LOW)
                .enqueue();
    }

    public void medial(View view) {
        new LogTask(this, "DEFOUT").setDuration(4000).setPriority(TaskPriority.DEFAULT).enqueue();
    }

    public void hight(View view) {
        new LogTask(this, "HIGHT")
                .setDuration(3000)
                .setPriority(TaskPriority.HIGH)
                .enqueue();
    }


    public void List(View view) {
        for (int i = 0; i < 5; i++) {
            LatLng latLng = new LatLng(28.12354532541, 113.25545544125);
            createMissionWaypoint(latLng);
        }

    }

    List<Byte> initMissionWaypoint = null;

    public void updateShowText(String s) {
        if (mStringBuffer.length() > 1000) {
            mStringBuffer.delete(0, mStringBuffer.length());
        }
        mStringBuffer.append(s).append("\n");
        mShowTask.setText(mStringBuffer.toString());
        Log.d("updateShowText", s + " \n");
    }


    /**
     * 创建航点任务
     */
    private void createMissionWaypoint(LatLng latLng) {
        initMissionWaypoint = new ArrayList<>();
        initMissionWaypoint.add(0, (byte) 0xFB);
        initMissionWaypoint.add(1, (byte) 0);
        Map<String, Double> latlng =
                LatLngUtils.delta(latLng.latitude, latLng.longitude);
        double Lon = RouteUtil.formatCustDouble(latlng.get("lon"), 6);
        double lat = RouteUtil.formatCustDouble(latlng.get("lat"), 6);
        //
        String longitudes = Lon + "";
        String latitudes = lat + "";
        Log.d("wayPoint", "经度长度:" + longitudes.length() + "");
        Log.d("wayPoint", "纬度长度:" + latitudes.length() + "");

        int jwlen = (longitudes.length()) << 4;
        jwlen += latitudes.length();

        Log.d("wayPoint", "经纬度长度:" + jwlen + "");
        Log.d("wayPoint", "经度:" + longitudes + "");
        Log.d("wayPoint", "纬度:" + latitudes + "");
        //1 经纬度长度
        initMissionWaypoint.add(2, (byte) jwlen);//0 1 2

        //3 纬度
        for (int j = 0; j < latitudes.getBytes().length; j++) {
            byte b2 = latitudes.getBytes()[j];
            initMissionWaypoint.add(initMissionWaypoint.size(), b2);
        }
        //2 经度
        for (int j = 0; j < longitudes.getBytes().length; j++) {
            byte b = longitudes.getBytes()[j];
            initMissionWaypoint.add(initMissionWaypoint.size(), b);
        }

        //4 高度
        int hightH = 120 / 256;
        int hightL = 120 % 256;
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) hightH);//高位
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) hightL);//低位

        //转动模式 偏航正负1

        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x01);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 90);
        //todo 是否有动作动作是否 1 动作次数 1
        int actionH;
        int actionL;
        actionH = 1;
        actionL = 1;
        int action = actionH << 4;
        action += actionL;
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) action);
        //动作个数
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x04);
        //动作指令
        byte[] comBytes = new byte[4];
        comBytes[0] = 0x00;
        comBytes[1] = 0x01;
        comBytes[2] = 0x04;
        comBytes[3] = 0x05;
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x00);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x01);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x04);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x05);
        //动作参数
        byte[] valueBytes = new byte[4];
        valueBytes[0] = 0x02;
        valueBytes[1] = 0x00;
        valueBytes[2] = 90;//90度
        valueBytes[3] = 50;
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x02);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0x00);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 90);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 50);
        int len = initMissionWaypoint.size();
        Log.d("wayPoint", "BPF 航点数据长度: " + len);
        //计算长度
        initMissionWaypoint.set(1, (byte) (len + 3));
        //计算CRC值
        int crc2 = CRC16.getCRC(initMissionWaypoint, initMissionWaypoint.size());
        int crchight = crc2 / 256;
        int crclow = crc2 % 256;
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) crchight);
        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) crclow);

        initMissionWaypoint.add(initMissionWaypoint.size(), (byte) 0xBB);

        for (int j = 0; j < initMissionWaypoint.size(); j++) {
            byte ob = initMissionWaypoint.get(j);
            new TCPTask(this, ob)
                    .setDuration(10)
                    .setPriority(TaskPriority.HIGH)
                    .enqueue();
        }

    }
}
